#include "BulletManager.h"

static BulletManager*		s_sharedBulletManager = NULL;


BulletManager* BulletManager::sharedBulletManager()
{
	if( s_sharedBulletManager == NULL ){
		s_sharedBulletManager = new BulletManager( );

		s_sharedBulletManager->setContentSize( CCDirector::sharedDirector()->getVisibleSize() );
	}

	return s_sharedBulletManager;
}


BulletManager::BulletManager( ) :
	m_pBatchNode(0),
	m_pListBullet(0),
	m_pListFocusBullet(new CCArray()),
	m_fBulletRadius(10)
{

}


BulletManager::~BulletManager( )
{
	if( m_pListBullet ){
		m_pListBullet->removeAllObjects( );
		m_pListBullet->release( );
	}

	if( m_pListFocusBullet) {
		m_pListFocusBullet->removeAllObjects( );
		m_pListFocusBullet->release( );
	}
}



void BulletManager::init( int numBullet )
{
	if( !m_pListBullet )
	{
		m_pListBullet = new CCArray(numBullet);
	}

	for( int i = 0 ; i < numBullet ; ++i)
	{
		Bullet* pBullet = new Bullet();
		pBullet->setVisible(false);
		pBullet->setTag(BULLET_UN_ACTIVE);
		m_pListBullet->addObject( pBullet );
		pBullet->release( );
	}

	
}


void BulletManager::initNextBullet( int numInit )
{
	if( !m_pListBullet ){
		init( numInit );
	}

	float chance = 0;

	for( int i = 0 ; i < numInit ; ++i )
	{
		Bullet* pBullet = getUnActiveBullet( );
		if( !pBullet ){
			pBullet = new Bullet();
			pBullet->setVisible(false);
			m_pListBullet->addObject( pBullet );
			pBullet->release( );
		}

		chance = Utility::sharedUtility()->getRandomInt( 1 , 5 );

		pBullet->setTag( BULLET_ACTIVE );
		pBullet->initBullet( kBulletTypeNormal  , (COLOR_TYPE)((int)chance) );
		pBullet->setRadius( m_fBulletRadius );
		pBullet->setPosition( ccp( this->getContentSize().width / 2 , this->getContentSize().height / 2 ) );

		pBullet->getPhysicObject()->setContactSolver(this);

		if( !pBullet->getParent( ) ){
			if( m_pBatchNode )
			{
				m_pBatchNode->addChild( pBullet );
			}else{
				this->addChild( pBullet );
			}
		}
		/*chance = Utility::sharedUtility()->getRandomInt( 0 , 100 );*/
	}
}


void BulletManager::getBullet( )
{
	Bullet* pBullet = getActiveBullet( );
	if(!pBullet ){
		initNextBullet(1);
		pBullet = getActiveBullet( );
	}

	pBullet->setVisible(true);

	m_pListFocusBullet->addObject( pBullet );
}


Bullet* BulletManager::getUnActiveBullet( )
{
	CCObject* pObj = NULL;
	CCARRAY_FOREACH( m_pListBullet , pObj )
	{
		Bullet* pBullet = dynamic_cast<Bullet*>(pObj);
		if( pBullet && pBullet->getTag() == BULLET_UN_ACTIVE )
		{
			return pBullet;
		}
	}

	return NULL;
}


Bullet* BulletManager::getActiveBullet( )
{
	CCObject* pObj = NULL;
	CCARRAY_FOREACH( m_pListBullet , pObj )
	{
		Bullet* pBullet = dynamic_cast<Bullet*>(pObj);
		if( pBullet && pBullet->getTag() == BULLET_ACTIVE )
		{
			return pBullet;
		}
	}

	return NULL;
}


void BulletManager::fire( const CCPoint& touchPoint )
{
	Bullet* pBullet = (Bullet*)m_pListFocusBullet->objectAtIndex(0);
	m_pListFocusBullet->removeObjectAtIndex(0);
	if( pBullet ){
		CCPoint direction = ccpSub( pBullet->getParent()->convertToNodeSpace( touchPoint ) , pBullet->getPosition() );
		direction = ccpNormalize( direction );
		
		pBullet->move(  direction  );

		pBullet->setTag( BULLET_IN_ACTION );
		pBullet->getPhysicObject()->setActive(true);
	}

	getBullet( );
}



void BulletManager::beginContact( PhysicObject* pContact_1 , PhysicObject* pContact_2 )
{
	Bullet* pBullet = dynamic_cast<Bullet*>(pContact_1->getObjectRef());
	CircleSegment* pCircle = dynamic_cast<CircleSegment*>(pContact_2->getObjectRef());

	if( pBullet && pCircle ){
		pBullet->unActive();

		if( pBullet->getColorType() == pCircle->getColorType() ){
			pCircle->unActive( );
		}
	}
}